﻿Public Class UCComboBox

    Public Event Changed()
    Public Event CreateClick(name As String)

    Public WriteOnly Property Values As IList(Of String)
        Set(value As IList(Of String))
            ComboBox1.DataSource = value
        End Set
    End Property
    Public ReadOnly Property Value As String
        Get
            If "" <> SelectedText Then Return SelectedText
            Return SelectedValue
        End Get
    End Property

    Public Property SelectedText As String
        Get
            Return ComboBox1.Text
        End Get
        Set(value As String)
            ComboBox1.Text = value
        End Set
    End Property
    Public Property SelectedValue As String
        Get
            Return ComboBox1.SelectedValue
        End Get
        Set(value As String)
            For Each i As String In ComboBox1.Items
                If i = value Then ComboBox1.SelectedIndex = ComboBox1.Items.IndexOf(i)
            Next
        End Set
    End Property
    Public Property Label As String
        Get
            Return Label1.Text
        End Get
        Set(value As String)
            Label1.Text = value
        End Set
    End Property
    Public Property LabelWidth As Integer
        Get
            Return Label1.Width
        End Get
        Set(value As Integer)
            Label1.Width = value
        End Set
    End Property



    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        If ComboBox1.Enabled Then RaiseEvent Changed()
    End Sub

    Private Sub ComboBox1_TextChanged(sender As Object, e As System.EventArgs) Handles ComboBox1.TextChanged
        btnCreate.Enabled = ComboBox1.Text <> ComboBox1.SelectedValue AndAlso ComboBox1.Text <> ""
        If btnCreate.Enabled Then RaiseEvent Changed()
    End Sub

    Private Sub btnCreate_Click(sender As Object, e As System.EventArgs) Handles btnCreate.Click
        RaiseEvent CreateClick(ComboBox1.Text)
    End Sub
End Class
